---
title: Hyperlink
icon: Link
---

<MetaData
  lang="en-US"
  meta={{
    preset: [{
      client: '@univerjs/preset-sheets-hyper-link',
      locale: '@univerjs/preset-sheets-hyper-link/locales/en-US',
      style: '@univerjs/preset-sheets-hyper-link/lib/index.css',
    }],
    plugins: [{
      client: '@univerjs/sheets-hyper-link',
      facade: '@univerjs/sheets-hyper-link/facade',
    }, {
      client: '@univerjs/sheets-hyper-link-ui',
      facade: '@univerjs/sheets-hyper-link-ui/facade',
      locale: '@univerjs/sheets-hyper-link-ui/locale/en-US',
      style: '@univerjs/sheets-hyper-link-ui/lib/index.css',
    }],
    server: 'No',
  }}
/>

Hyperlinks are used to quickly navigate and access content within spreadsheets, such as internal worksheets, cells, and external web pages or email addresses.

<PlaygroundFrame lang="en-US" slug="sheets/hyper-link" clickToShow />

## Preset Mode

### Installation

```package-install
npm install @univerjs/preset-sheets-hyper-link
```

### Usage

```typescript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import { UniverSheetsHyperLinkPreset } from '@univerjs/preset-sheets-hyper-link' // [!code ++]
import UniverPresetSheetsHyperLinkEnUS from '@univerjs/preset-sheets-hyper-link/locales/en-US' // [!code ++]
import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'

import '@univerjs/preset-sheets-core/lib/index.css'
import '@univerjs/preset-sheets-hyper-link/lib/index.css' // [!code ++]

const { univerAPI } = createUniver({
  locale: LocaleType.En_US,
  locales: {
    [LocaleType.En_US]: mergeLocales(
      UniverPresetSheetsCoreEnUS,
      UniverPresetSheetsHyperLinkEnUS, // [!code ++]
    ),
  },
  presets: [
    UniverSheetsCorePreset(),
    UniverSheetsHyperLinkPreset(), // [!code ++]
  ],
})
```

### Presets and Configuration

```typescript
UniverSheetsHyperLinkPreset({
  // Customize the way external links are opened
  urlHandler: {
    navigateToOtherWebsite: url => window.open(`${url}?utm_source=univer`, '_blank'),
  },
})
```

Complete configuration options can be found in [`IUniverSheetsHyperLinkPresetConfig`](https://github.com/dream-num/univer-presets/blob/dev/packages/preset-sheets-hyper-link/src/umd.ts#L14).

## Plugin Mode

### Installation

```package-install
npm install @univerjs/sheets-hyper-link @univerjs/sheets-hyper-link-ui
```

### Usage

```typescript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'
import { UniverSheetsHyperLinkPlugin } from '@univerjs/sheets-hyper-link' // [!code ++]
import { UniverSheetsHyperLinkUIPlugin } from '@univerjs/sheets-hyper-link-ui' // [!code ++]
import SheetsHyperLinkUIEnUS from '@univerjs/sheets-hyper-link-ui/locale/en-US' // [!code ++]

import '@univerjs/sheets-hyper-link-ui/lib/index.css' // [!code ++]

import '@univerjs/sheets-hyper-link/facade' // [!code ++]
import '@univerjs/sheets-hyper-link-ui/facade' // [!code ++]

const univer = new Univer({
  locale: LocaleType.En_US,
  locales: {
    [LocaleType.En_US]: mergeLocales(
      SheetsHyperLinkUIEnUS, // [!code ++]
    ),
  },
})

univer.registerPlugin(UniverSheetsHyperLinkPlugin) // [!code ++]
univer.registerPlugin(UniverSheetsHyperLinkUIPlugin) // [!code ++]
```

### Plugins and Configuration

```typescript
univer.registerPlugin(UniverSheetsHyperLinkUIPlugin, {
  // Customize the way external links are opened
  urlHandler: {
    navigateToOtherWebsite: url => window.open(`${url}?utm_source=univer`, '_blank'),
  },
})
```

Complete configuration options can be found in [`IUniverSheetsHyperLinkUIConfig`](https://github.com/dream-num/univer/blob/dev/packages/sheets-hyper-link-ui/src/controllers/config.schema.ts#L26).

## Facade API

Complete Facade API type definitions can be found in the [FacadeAPI](https://reference.univer.ai/en-US).

### Build/Parse/Jump HyperLink

```typescript
const fWorkbook = univerAPI.getActiveWorkbook()
const sheets = fWorkbook.getSheets()

// Create a hyperlink to the cell F6 in the first sheet
const sheet1 = sheets[0]
const range = sheet1.getRange('F6')
const hyperlink = range.getUrl()

// Parse the hyperlink
const hyperlinkInfo = fWorkbook.parseSheetHyperlink(hyperlink)

// Switch to the second sheet
fWorkbook.setActiveSheet(sheets[1])
fWorkbook.getActiveSheet().getSheetName()

// Navigate to the hyperlink after 3 seconds
setTimeout(() => {
  fWorkbook.navigateToSheetHyperlink(hyperlink)
  fWorkbook.getActiveSheet().getSheetName()
}, 3000)
```

### Manipulating Hyperlinks in Cells

```typescript
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Create a hyperlink to Univer on cell A1
const fRange = fWorksheet.getRange('A1')
const richText = univerAPI.newRichText().insertLink('Univer', 'https://univer.ai/')
fRange.setRichTextValueForCell(richText)

// Query the hyperlinks in the current cell
const cellValue = fRange.getValue(true)
const hyperlinks = cellValue.getLinks()

// Update hyperlink after 3 seconds
setTimeout(() => {
  const id = hyperlinks[0].rangeId
  const newUrl = 'https://insight.univer.ai/'
  const newRichText = cellValue.copy().updateLink(id, newUrl)
  fRange.setRichTextValueForCell(newRichText)
}, 3000)

// Cancel hyperlink after 6 seconds
setTimeout(() => {
  const newCellValue = fRange.getValue(true)
  const newHyperlinks = newCellValue.getLinks()
  const id = newHyperlinks[0].rangeId
  const newRichText = newCellValue.copy().cancelLink(id)
  fRange.setRichTextValueForCell(newRichText)
}, 6000)
```

### Event Listeners

Full event type definitions, please refer to [Events](https://reference.univer.ai/en-US/classes/FEventName).

You can listen to hyperlink-related events using `univerAPI.addEvent()`:

#### Link Addition Event

`univerAPI.Event.BeforeSheetLinkAdd`: Triggered before adding a link

```typescript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetLinkAdd, (params) => {
  const { workbook, worksheet, row, col, link } = params

  // Cancel the sheet link add operation
  params.cancel = true
})

// Remove the event listener, use `disposable.dispose()`
```

#### Link Update Event

`univerAPI.Event.BeforeSheetLinkUpdate`: Triggered before updating a link

```typescript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetLinkUpdate, (params) => {
  const { workbook, worksheet, row, column, id, payload } = params

  // Cancel the sheet link update operation
  params.cancel = true
})

// Remove the event listener, use `disposable.dispose()`
```

#### Link Deletion Event

`univerAPI.Event.BeforeSheetLinkCancel`: Triggered before canceling a link

```typescript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetLinkCancel, (params) => {
  const { workbook, worksheet, row, column, id } = params

  // Cancel the sheet link cancel operation
  params.cancel = true
})

// Remove the event listener, use `disposable.dispose()`
```

Each event includes the following common parameters:
- `workbook`: Current workbook instance
- `worksheet`: Current worksheet instance
- `row`: Row index of the cell containing the link
- `column`: Column index of the cell containing the link

Special parameters:
- `BeforeSheetLinkAdd` event includes `link`: The link to be added
- `BeforeSheetLinkUpdate` event includes:
  - `id`: Link identifier
  - `payload`: New link data
- `BeforeSheetLinkCancel` event includes `id`: Link identifier to be removed

All event callbacks can use `params.cancel = true` to prevent the corresponding operation.
